//******Google APi setting 
var clientId = '14806957832.apps.googleusercontent.com'; //jb-shhb->'828062657182-a2i7dlpvhlv2uqe4f101i1430qh1thmr.apps.googleusercontent.com';
var apiKey = 'AIzaSyBrR0uxe3x6G2-ntujf9YURrfE5R9QT728'; //jb-shhb->'AIzaSyCZtTH-e9ZXKkNX07j7RzTtDttNS_JrwfQ';
var scopes = ''; // 'https://www.googleapis.com/auth/plus.me';
var MY_API_ROOT = 'https://ediexplorer.appspot.com/_ah/api';
var MY_API_VER = 'v1';
var MY_API_NAME = 'ediexp';
var elementCounter= 0;
//$.getScript('https://apis.google.com/js/client.js', function() {        
//	  $('body').append('<p>GOOGLE API  is loaded</p>');
//});

window.onload = function() {
    //gapi.client.setApiKey(apiKey);
    gapi.client.load(MY_API_NAME, MY_API_VER, null, MY_API_ROOT);
}

function getSegment(seqId){
	if(seqId!='undefined'){
		var request = gapi.client.ediexp.segment.get({
			'id': 'X.005010.' + seqId
		});
		 request.execute(function(jsonResp, rawResp) {
			 elementCounter= 0;
			 var html = '<div class="textlarge">'+ jsonResp.name +'</div>';
			 html += '<div class="textNormal">'+ jsonResp.description +'</div>';
			 html += createSeqElements(jsonResp.elementList,seqId);
			 $("#"+ ROOT_ID + '_divbody').append(html);
			 
		 });
	}
}

function createSeqElements(data,seqId){
	var strReturn ='';
	strReturn += '<div class="elementsBox">' ;
	for (var i = 0; i < data.length; i++){
		
		strReturn += '<div class="elementBox' ; 
		if(data[i].usage==='Required')
			strReturn += ' required' ;
		else if(data[i].usage==='NotUsed')
			strReturn += ' notUsed' ;
		else if(data[i].usage==='Situational')
			strReturn += ' Situational' ;
		else if(data[i].usage==='Optional')
			strReturn += ' optional' ;
		
		strReturn += '"  id="' + data[i].id + '"  onclick="getInfo(\''+ data[i].id + '\')" >' ;
		
		strReturn += '</div>';
		returnDEInfo(data[i].id,data[i].elementId,seqId,data[i].usage);
	}
	strReturn += '</div>' ;
	return strReturn;
}

function returnDEInfo(id, dataElementId,seqId,usage){
	if(dataElementId!='undefined'){
		var request = gapi.client.ediexp.de.get({
			'id':  dataElementId
		});
		 request.execute(function(jsonResp, rawResp) {
			 elementCounter += 1;
			 var strReturn ='';
				strReturn += '<table  height="70px" width="120px" border="0">';
					strReturn += '<tr>';
						strReturn += '<td width="33%" align="left"><span class="textXSmall">' +  seqId + elementCounter +'</span></td>' ;
						strReturn += '<td width="33%"><span class="textSmall"></span></td>' ;
						strReturn += '<td width="33%" align="right"><span class="textXSmall">' + trimElement(dataElementId) +'</span></td>' ;
					strReturn += '</tr>';
					strReturn += '<tr >';
						strReturn += '<td style="border-top:1px dotted #d3d3cc;border-bottom:1px dotted #d3d3cc" align="center" colspan="3"><span class="textSmall';
						if(usage==='NotUsed')
							strReturn += ' textUnused ' ;
						strReturn += '">' + jsonResp.name +'</span></td>' ;
					strReturn += '</tr>';
					strReturn += '<tr>';
						strReturn += '<td align="left"><span class="textXSmall" >' + 'M' +'</span></td>' ;
						strReturn += '<td align="center"><span class="textXSmall">' + jsonResp.type +'</span></td>' ;
						strReturn += '<td align="right"><span class="textXSmall" >' + jsonResp.minLength + '/' + jsonResp.maxLength +'</span></td>' ;
					strReturn += '</tr>';				
				strReturn += '</table>';
				var object = document.getElementById(id);
				 $(object).append(strReturn);
		 });
	}
}

function trimElement(element){
	return element.split('.')[2];
}

function getDataElement(dedat,segdata ){
	if(dedat!='undefined'){
		var request = gapi.client.ediexp.segment.getDE({
			'dedata': dedat ,
			'segdata': segdata
		});
		 request.execute(function(jsonResp, rawResp) {
			 var html = '<table><tr><td width="50%"><div class="textlarge">'+ jsonResp.name +'</div></td>';
			 html += '<td width="100px" style="text-align:center"><div class="textNormal">'+ jsonResp.type +'</div></td>';
			 html += '<td width="1px"><div class="textNormal">'+ jsonResp.minLength +'</div></td>';
			 html += '<td width="1px">/</td>';
			 html += '<td width="1px"><div class="textNormal">'+ jsonResp.maxLength +'</div></td></tr></table>';
			 html += '<div class="textNormal">'+ jsonResp.description +'</div>';
		
			 $("#"+ ROOT_ID + '_divbody').append(html);
		 });
	}
}